var fileInput = null;
var mowerFileInputReader = null;
var LOGGER;

/**
 * @public
 */
function main() {
	LOGGER = new OnPageLogger($("#outputConsole"));
	initDisplay();
	fileInput = $("#fileInput");
	fileInput.change(onFileInputChanged);	
};

/**
 * @private
 */
function onFileInputChanged() {
	displayLoading();
	var file = fileInput.prop("files")[0];
	mowerFileInputReader = new MowerFileInputReader(file, function(){
		onFileLoaded(fileInput.val());
	});	
};

/**
 * @param {string} filename
 * @private
 */
function onFileLoaded(filename) {
	displayLoadingFinished();
	var mowerDriver = new MowerDriver(mowerFileInputReader, outputDriveResult);
	try {
		mowerDriver.driveMowers();
	} catch(e) {
		LOGGER.log(e);
	}
};

/**
 * @param {MowerDriveResult} driveResult
 * @private
 */
function outputDriveResult(driveResult) {
	$("<span>" + driveResult.toOutputString() + "<br></span>").appendTo($("#resultsOuput"));
};

/**
 * @private
 */
function initDisplay() {
	$("#fileInputFake").click(function(e){
		fileInput.trigger("click");
	});
	$("#resultsOuput").empty();
	$("#filename").empty();
};

/**
 * @private
 */
function displayLoading() {
	$("#fileInputFake").attr("disabled", "disabled");
	$("#resultsOuput").html("Loading file...");	
	$("#filename").empty();
	$("#outputConsole").empty();
};

/**
 * @private
 */
function displayLoadingFinished() {
	$("#resultsOuput").empty();
	$("#filename").html(fileInput.val());
	$("#fileInputFake").removeAttr("disabled");
};